package com.huike.common.service.Impl;

import com.huike.common.annotation.Log;
import com.huike.common.config.MinioConfig;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.service.ISysFileService;
import com.huike.common.utils.file.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class SysFileServiceImpl implements ISysFileService {
    @Autowired
    MinioConfig minioConfig;

    /**
     * 文件上传
     */
    @Override
    public AjaxResult upload(MultipartFile file) {
        MinioUtil miniUtil = new MinioUtil();
        InputStream inputStream = null;
        String bucketName = minioConfig.getBucketName();
        try {
            inputStream = file.getInputStream();
            //如果桶不存在--则删除桶
            if (!miniUtil.bucketExists(bucketName)) {
                miniUtil.makeBucket(bucketName);
            }
            String filename = file.getOriginalFilename();
            String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + UUID.randomUUID().toString().replaceAll("-", "")
                    + filename.substring(filename.lastIndexOf("."));
            miniUtil.putObject(bucketName, objectName, inputStream);
            String url = miniUtil.getObjectUrl(bucketName, objectName);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", "/" + bucketName + "/" + objectName);
            //url需要进行拦截
            ajax.put("url", url);
            return ajax;

        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        } finally {
            //防止内存泄露
            if (inputStream != null) {
                try {
                    //关流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.debug("inputStream close IOException:" + e.getMessage());
                }
            }
        }

    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     * @return
     */
    @Override
    public AjaxResult downloadByMinio(String fileName, HttpServletResponse response) {
        try {


            String bucketName = minioConfig.getBucketName();
            MinioUtil minioUtil = new MinioUtil();
            String url = minioUtil.getObjectUrl(bucketName, fileName);
            minioUtil.downloadFile(bucketName, fileName, "clues.xlsx", response);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
